<template>
    <div class="meeting-detail-container">
        <div class="meeting-detail" :style="{'background-image': `url(${ meetingInfo.coverImagePath })`}">
            <template v-if="meetingInfo.meetingStatus === 0">
                <div class="card-content">
                    <div class="status">- 直播未开始 -</div>
                    <div class="button primary" @click="goMeetingHandler">
                        <span>进入直播</span>
                    </div>
                </div>
            </template>
            <template v-else-if="meetingInfo.meetingStatus === 1">
                <div class="card-content">
                    <div class="status">- 直播进行中 -</div>
                    <div class="button primary" @click="goMeetingHandler">
                        <span>进入直播</span>
                    </div>
                </div>
            </template>
            <template v-else-if="meetingInfo.meetingStatus === 2">
<!--                <div :class="cardContentClassNames">-->
<!--                    <template v-if="meetingInfo.recordStatus === 0">-->
<!--                        <div class="status">- 直播已结束 无回放 -</div>-->
<!--                    </template>-->
<!--                    <template v-else-if="meetingInfo.recordStatus === 1">-->
<!--                        <div class="status">- 回放生成中 -</div>-->
<!--                    </template>-->
<!--                    <template v-else-if="meetingInfo.recordStatus === 2">-->
<!--                        <div class="status">- 直播已结束 -</div>-->
<!--                        <div class="button primary" @click="onclickPlayReplayHandler">-->
<!--                            <span>观看回放</span>-->
<!--                        </div>-->
<!--                    </template>-->
<!--                </div>-->

                <template v-if="meetingInfo.recordStatus === 0">
                    <div :class="cardContentClassNames">
                        <div class="status">- 直播已结束 无回放 -</div>
                    </div>
                </template>
                <template v-else-if="meetingInfo.recordStatus === 1">
                    <div :class="cardContentClassNames">
                        <div class="status">- 回放生成中 -</div>
                    </div>
                </template>

                <template v-else-if="meetingInfo.recordStatus === 2">
                    <div :class="screenClassNames">
                        <div class="screen-video">
                            <MaterialResPlayer
                                mode="h5"
                                :material="material"
                                :video-config="videoConfig"
                                @play="handlePlaying"
                            />
                        </div>
                    </div>
                </template>
            </template>
        </div>
        <div class="meeting-intro">
            <div class="meeting-intro-title">
                <p>{{ meetingInfo.meetingName }}</p>
            </div>
            <div class="meeting-intro-time">
                <span>开始时间：{{ meetingStartTime }}</span>
            </div>
            <div class="meeting-intro-duration">
                <span>{{ meetingDuration }}</span>
            </div>
        </div>

        <van-popup
            v-model:show="forbidDialogState.forbidDialogVisible"
            class="forbid-dialog"
            position="bottom"
            round
        >
            <div class="forbid-dialog-main">
                <div class="title">提示</div>
                <div class="content">
                    <img src="~@/assets/images/no-permission.png" alt="">
                    <p>{{ forbidDialogState.forbidMessage1 }}</p>
                    <p>{{ forbidDialogState.forbidMessage2 }}</p>
                    <van-button class="i-know-btn" @click="forbidDialogState.forbidDialogVisible = false">知道了</van-button>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script setup>
import {MaterialResPlayer} from '@yanxiu/player';
import dayjs from 'dayjs';
import {computed, reactive, ref, unref} from 'vue';
import {useRoute} from 'vue-router';
import {useStore} from 'vuex';
import {isApp} from '@/lib/judge-env.js';
import {pageJump} from '@/global/location-transfer';

const props = defineProps({
    meetingInfo: {
        type: Object,
        default: () => ({})
    },
})

const route = useRoute();
const store = useStore();

function formatDuration(duration) {
    function formatNumber(value) {
        return `${`${value}`.length > 1 ? value : `0${value}`}`;
    }

    if (duration < 60) {
        return `00:00:${formatNumber(duration)}`;
    } else if (duration < 60 * 60) {
        return `00:${formatNumber(Math.floor(duration / 60))}:${formatNumber(duration % 60)}`;
    } else {
        return `${Math.floor(duration / 60 / 60)}:${formatNumber(Math.floor((duration - Math.floor(duration / 60 / 60) * 3600) / 60))}:${formatNumber(duration % 60)}`;
    }
}

const meetingStartTime = computed(() => {
    const time = props.meetingInfo.meetingStatus ? props.meetingInfo.realStartTime : props.meetingInfo.startTime;
    return dayjs(time).format('YYYY.MM.DD HH:mm');
})
const meetingDuration = computed(() => {
    if (props.meetingInfo.meetingStatus === 2) {
        return `会议时长：${formatDuration(props.meetingInfo.actualDuration)}`
    }

    return `预计时长：${props.meetingInfo.expectDuration}小时`
})

const cardContentClassNames = ref(['card-content']);
const screenClassNames = ref(['screen']);

const brand = computed(() => {
    return store.state?.brand.publishBrand || {}
})

const currentUser = computed(() => {
    return store.state?.currentUser || {};
});
const isMaster = computed(() => {
    return currentUser.value.userId === props.meetingInfo.host.userId
});

const forbidDialogState = reactive({
    forbidMessage1: '您是会议主持人',
    forbidMessage2: '请从电脑端或APP端访问会议',
    forbidDialogVisible: false,
})

function goMeetingHandler() {
    const meetingRoomUrl = props.meetingInfo.roomUrl;
    const meetingId = props.meetingInfo.meetingId;
    const moocId = route.params.moocId;
    const { from, fid, courseId, catalogueId } = route.query;
    const brandKey = unref(brand).brand;

    if (isApp()) {
        store.dispatch('jsBridge/SXYJSBridge', {
            method: 'jumpToMeetingRoom',
            data: {
                meetingId: meetingId,
                dtBrandId: brandKey,
                moocId,
                kmsId: courseId,
            }
        })
    } else {
        if (unref(isMaster)) {
            forbidDialogState.forbidDialogVisible = true;
        } else {
            // /agora/meeting/:meetingID/room?brand=:brand&moocId=:moocId&lessonFrom=:from&lessonFid=:fid&kmsId=:kmsId&lessonCatalogueId=:catalogueId
            pageJump(`${meetingRoomUrl}&moocId=${moocId}&lessonFrom=${from}&lessonFid=${fid}&kmsId=${courseId}&lessonCatalogueId=${catalogueId}`);

        }
   }
}

const material = computed(() => {
    if (props.meetingInfo.recordStatus === 2) {
        return {
            materialStatus: 'usable',
            materialType: 'video',
            previewUrl: props.meetingInfo.pushPullInfo.recAddrPc,
        }
    }

    return {}
});
const videoConfig = computed(() => {
    if (material.value.materialStatus === 'usable' && route.query.resourceStart) {
        return {
            lastPlayTime: route.query.resourceStart,
            autoplay: true
        }
    }

    return {}
});
function onclickPlayReplayHandler() {
    // cardContentClassNames.value = ['card-content', 'card-content-fadeout'];
    // screenClassNames.value = ['screen', 'screen-fadein'];
    //
    // // playing
    // material.value = {
    //     materialStatus: 'usable',
    //     materialType: 'video',
    //     previewUrl: props.meetingInfo.pushPullInfo.recAddrPc,
    // }
}

function handlePlaying() {
    G_transmit('viewRecord', {
        queries: {
            moocId: route.params.moocId
        }
    })
}

</script>

<style lang="scss" scoped>
.meeting-detail-container {
    height: 100%;
    background: #FFFFFF;

    .meeting-detail {
        height: 210px;
        width: 100%;
        position: relative;
        background-repeat: no-repeat;
        background-size: cover;

        .card-content {
            background: rgba(0, 0, 0, 0.5);
            height: 100%;
            width: 100%;
            display: flex;
            font-size: 14px;
            color: #FFF;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            .status {
                margin-bottom: 8px;
            }
            .button {
                padding: 0 15px;
                height: 32px;
                vertical-align: middle;
                line-height: 32px;
                text-align: center;
                color: #FFF;
                border-radius: 2px;
                position: relative;

                &.primary {
                    background: #0084F6;
                }

                &::before {
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    width: 100%;
                    height: 100%;
                    background-color: #000;
                    border-color: #000;
                    border-radius: inherit; /* inherit parent's border radius */
                    transform: translate(-50%, -50%);
                    opacity: 0;
                    content: "";
                }
                &:active::before {
                    opacity: 0.1;
                }
            }
        }
        .screen {
            position: absolute;
            left: 0;
            top: 0;
            //z-index: -1;
            height: 100%;
            width: 100%;
            //opacity: 0;

            .screen-video {
                height: 100%;
            }
        }

        .card-content-fadeout {
            animation-name: fadeOut;
            animation-duration: 1s;
            animation-fill-mode: both;
        }
        .screen-fadein {
            animation-name: fadeIn;
            animation-duration: 1s;
            animation-fill-mode: both;
        }
    }

    .meeting-intro {
        padding: 0 15px;
        font-family: "PingFang SC", sans-serif;
        font-size: 15px;
        color: #171A1D;

        .meeting-intro-title {
            padding: 15px 0;
            line-height: 26px;
            font-weight: bold;
            font-size: 18px;
        }

        .meeting-intro-time,
        .meeting-intro-duration {
            line-height: 24px;
        }
    }
}

.forbid-dialog {
    border-radius: 8px 8px 0 0;
}
.forbid-dialog-main {
    .title {
        height: 48px;
        display: flex;
        justify-content: center;
        align-items: center;
        line-height: 24px;
        font-weight: 600;
        font-size: 16px;
        color: #171A1D;
        border-bottom: 1px solid #EEE;
    }

    .content {
        padding: 30px 15px 8px;
        text-align: center;

        img {
            width: 260px;
            margin-bottom: 12px;
        }
        p {
            line-height: 22px;
            font-size: 16px;
            color: #6E7074;
        }

        .i-know-btn {
            margin-top: 30px;
            width: 100%;
            background: #EFF0F2;
            border-radius: 4px;
        }
    }
}

@keyframes fadeOut {
    0% {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}
@keyframes fadeIn {
    0% {
        opacity: 0;
        z-index: -1;
    }
    to {
        opacity: 1;
        z-index: 1;
    }
}
</style>